local leigu = fk.CreateSkill{
  name = "doublez__leigu",
  tags = {
    Skill.Compulsory
  }
}

Fk:loadTranslationTable{
  ["doublez__leigu"] = "擂鼓",
  [":doublez__leigu"] = "锁定技，回合最后一个阶段结束后，你执行一个出牌阶段或摸牌阶段（不能与此阶段相同）,"..
    "该摸牌/出牌阶段结束时，若此阶段你的手牌数没有摸至X张/没有失去恰好X张手牌，你失去1点体力或结束此回合（X为本回合你因此执行的阶段数）。",
  
  ["doublez__leigu_loseHp"] = "失去1点体力",
  ["doublez__leigu_endTurn"] = "结束此回合",
  ["@doublez__leigu_lose-phase"] = "擂鼓 失去",
  ["@doublez__leigu-turn"] = "擂鼓",

  ["$doublez__leigu1"] = "博古揽今，信手拈来。",
  ["$doublez__leigu2"] = "功名为尘，光阴为金。",
}

leigu:addEffect(fk.EventPhaseEnd, {
  priority = 2,
  can_trigger = function (self, event, target, player, data)
    if not( player:hasSkill(leigu.name) and target == player and data.reason == leigu.name) then
      return
    end
    if data.phase == Player.Draw then
      return player:getHandcardNum() ~= player:getMark("@doublez__leigu-turn")
    else
      return player:getMark("@doublez__leigu_lose-phase") ~= player:getMark("@doublez__leigu-turn")
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = {
      "doublez__leigu_loseHp",
      "doublez__leigu_endTurn"
    }
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = leigu.name,
      cancelable = false
    })
    if choice then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local choice = event:getCostData(self)
    local room = player.room
    if choice == "doublez__leigu_loseHp" then
      room:loseHp(player, 1, leigu.name)
    elseif choice == "doublez__leigu_endTurn" then
      room:endTurn()
    end
  end
})

leigu:addEffect(fk.EventPhaseEnd, {
  priority = 1,
  can_trigger = function (self, event, target, player, data)
    local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    return player:hasSkill(leigu.name) and target == player and turn_event and turn_event.data and turn_event.data.phase_index == #turn_event.data.phase_table
  end,
  on_cost = function (self, event, target, player, data)
    if data.phase == Player.Draw then
      event:setCostData(self, "phase_play")
    elseif data.phase == Player.Play then 
      event:setCostData(self, "phase_draw")
    else
      local choices = {
        "phase_draw",
        "phase_play"
      }
      local choice = player.room:askToChoice(player, {
        choices = choices,
        skill_name = leigu.name,
        cancelable = false
      })
      if choice then
        event:setCostData(self, choice)
      end
    end
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@doublez__leigu-turn", 1)
    local choice = event:getCostData(self)
    if choice == "phase_draw" then
      player:gainAnExtraPhase(Player.Draw, leigu.name)
    elseif choice == "phase_play" then
      player:gainAnExtraPhase(Player.Play, leigu.name)
    end
  end
})

leigu:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  can_refresh = function (self, event, target, player, data)
    local phase_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Phase, true)
    return player:hasSkill(leigu.name) and phase_event and phase_event.data 
      and phase_event.data.reason == leigu.name and phase_event.data.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand then
            player.room:addPlayerMark(player, "@doublez__leigu_lose-phase", 1)
          end
        end
      end
    end
  end
})

return leigu